/*
 * Registration.cpp
 *
 *  Created on: Sep 12, 2013
 *      Author: stepgalvao
 */

#include "RGImage.h"
#include <iostream>
#include "RGFunction.h"

/**
 * Display a ploted image. The features are ploted in image.
 */
void ShowFeatureImg(RGImage img1, String name);

int main(int argc, char** argv) {

	/*
	 * Getting Imagens
	 *	The mov image, that will warp.
	 *	The ref image, that will be the reference for register.
	 */
	RGImage mov = RGImage(
			"/home/stepgalvao/Doutorado/Banco/Antigo/Paciente01/01.jpg");
	RGImage ref = RGImage(
			"/home/stepgalvao/Doutorado/Banco/Antigo/Paciente01/02.jpg");

	/*
	 * Show the features identify in the two images.
	 */
	ShowFeatureImg(mov, "MovingFeatures.jpg");
	ShowFeatureImg(ref, "RefenceFeatures.jpg");
	/*
	 * Calculating the transform function
	 */
	RGFunction function;

	/*
	 * Transformed Move Image .
	 */
	Mat warp_dst = Mat::zeros(mov.GetSize(), CV_8U);

	/*
	 * Affine Transform Matrix.
	 */
	Mat warp_mat(2, 3, CV_64F);

	/*
	 * Calculating the transform function
	 */
	warp_mat = function.llsq(mov.GetFeaturesAsPoints(),
			ref.GetFeaturesAsPoints());
	//warp_mat= getAffineTransform(mov.GetFeatures(),mod.GetFeatures());
	//cout<< warp_mat<<endl;

	warpAffine(mov.GetImage(), warp_dst, warp_mat, warp_dst.size());

	Mat result = abs(warp_dst - mov.GetImage());

	RGImage::ShowImage("Mov.jpg", mov.GetImage());
	RGImage::ShowImage("Reference.jpg", ref.GetImage());
	RGImage::ShowImage("Warp.jpg", warp_dst);
	RGImage::ShowImage("SubReferenceWarp.jpg", abs(warp_dst - ref.GetImage()));
	RGImage::ShowImage("SubMovReference.jpg", abs(mov.GetImage() - ref.GetImage()));
	RGImage::ShowImage("SubWarpReference.jpg", abs(warp_dst - ref.GetImage()));

	waitKey(0);

	return 0;
}

void ShowFeatureImg(RGImage img1, String name) {
	Mat imgPloted(img1.GetSize(), CV_8U);
	vector<KeyPoint> teste = img1.GetFeatures();
	drawKeypoints(img1.GetImage(), img1.GetFeatures(), imgPloted);
	RGImage::ShowImage(name, imgPloted);
}

